package com.societyGames.flashForms
{
import flash.display.Sprite;
import flash.events.Event;

public class TabItemsContainer extends Sprite
{
  private var _desiredHeight:Number;
  private var _desiredWidth:Number;
  private var _tabItems:Array = []; //TabItem
  private var _selectedTabItem:TabItem;
  private var _headersContainer:HeadersContainer;
  private var _headerHeightUnits:Number;
  private var _headerHeightUnitType:DockUnitType;

  public function TabItemsContainer(headerHeight:Number, headerHeightUnitType:DockUnitType)
  {
    if (headerHeightUnitType != DockUnitType.pixel && headerHeightUnitType != DockUnitType.percent)
    {
      throw new ArgumentError("DockUnitType not supported.");
    }
    _headerHeightUnits = headerHeight;
    _headerHeightUnitType = headerHeightUnitType;
    this._headersContainer = new HeadersContainer();
    this.addChild(this._headersContainer);
  }

  public override function set width(value:Number):void
  {
    if (value != this._desiredWidth)
    {
      this._desiredWidth = value;
      recalculateWidth();
    }
  }

  private function recalculateWidth():void
  {
    this._headersContainer.width = _desiredWidth;
    if (_selectedTabItem != null)
    {
      this._selectedTabItem.content.width = this._desiredWidth;
    }
  }

  public override function set height(value:Number):void
  {
    if (value != this._desiredHeight)
    {
      this._desiredHeight = value;
      recalculateHeight();
    }
  }

  private function recalculateHeight():void
  {
    var headerHeight:Number = _headerHeightUnitType == DockUnitType.pixel ? _headerHeightUnits : this._desiredHeight * _headerHeightUnits;
    var availableContentHeight:Number = this._desiredHeight - headerHeight;
    //Shrink the content or remove it altogether.
    if (this._selectedTabItem != null)
    {
      if (availableContentHeight > 0)
      {
        this._selectedTabItem.content.visible = true;
        this._selectedTabItem.content.height = availableContentHeight;
        this._selectedTabItem.content.y = headerHeight;
      }
      else
      {
        this._selectedTabItem.content.visible = false;
        this._selectedTabItem.content.height = 0;
      }
    }
    //If there's enough room, let the header take up as much as it wants.
    if (availableContentHeight >= 0)
    {
      this._headersContainer.height = headerHeight;
    }
    else
    {
      this._headersContainer.height = this._desiredHeight;
    }
  }

  public function addTabItem(tabItem:TabItem):void
  {
    //Keep in main list.
    this._tabItems.push(tabItem);

    //Add to render list and listen for click.
    this._headersContainer.addHeader(tabItem.headerButton);
    this.addChild(tabItem.content);
    tabItem.headerClickedEventObject.addEventListener(tabHeaderClickedHandler);

    //Prep the tab item content
    tabItem.content.visible = false;
    tabItem.content.x = 0;

    //Select if there isn't one.
    if (this._selectedTabItem == null)
    {
      selectTabItem(tabItem);
    }
    else
    {
      tabItem.deselect();
      recalculateHeight();
    }
  }

  public function get tabItemCount():int
  {
    return this._tabItems.length;
  }

  public function getTabItem(index:int):TabItem
  {
    return TabItem(this._tabItems[index]);
  }

  public function removeTabItem(tabItem:TabItem):void
  {
    //See if the tab is present.
    var index:int = this._tabItems.indexOf(tabItem);
    if (index != -1)
    {
      //remove from main list.
      this._tabItems.splice(index, 1);

      //Stop rendering
      this._headersContainer.removeHeader(tabItem.headerButton);
      this.removeChild(tabItem.content);

      //Don't respond to clicks anymore
      tabItem.headerClickedEventObject.removeEventListener(tabHeaderClickedHandler);

      //Choose the first tab if we were selected.
      if (this._selectedTabItem == tabItem)
      {
        if (this._tabItems.length > 0)
        {
          selectTabItem(this._tabItems[0]);
        }
        else //Just clear it to nothing.
        {
          selectTabItem(null);
        }
      }
    }
  }

  public function selectTabItem(tabItem:TabItem):void
  {
    if (this._selectedTabItem != tabItem)
    {
      if (this._selectedTabItem != null)
      {
        this._selectedTabItem.deselect();
        this._selectedTabItem.content.visible = false;
      }
      if (tabItem != null)
      {
        //Move us to the front. We better be in the render list.
        this._headersContainer.selectHeader(tabItem.headerButton);
        this.removeChild(tabItem.content);
        this.addChild(tabItem.content);
        tabItem.select();
        tabItem.content.visible = true;
        tabItem.content.x = 0;
      }
      this._selectedTabItem = tabItem;

      //Rekick the sizing
      recalculateHeight();
      recalculateWidth();
    }
  }

  private function tabHeaderClickedHandler(event:Event):void
  {
    selectTabItem(TabItem(event.target));
  }
}
}